package lol.clann.minecraft.springboot.plugin.command;

import lol.clann.minecraft.springboot.api.bukkit.command.TabComplete;
import lol.clann.minecraft.springboot.api.annotation.command.Command;
import lol.clann.minecraft.springboot.api.annotation.command.CommandDefine;
import lol.clann.minecraft.springboot.api.annotation.command.Sender;
import lol.clann.minecraft.springboot.api.context.PluginContext;
import lol.clann.minecraft.springboot.api.model.core.Plugin;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 重载SBP插件配置的指令
 *
 * @author pyz
 * @date 2019/5/6 11:57 PM
 */
@CommandDefine(name = "springboot", description = "插件框架", aliases = "sbp", order = -1)
public class PluginReloader {

    @Command(showArgs = "reload <PluginId>", des = "重载SpringBoot插件", tabComplete = PluginTabComplete.class)
    private void reload(@Sender CommandSender sender, Plugin plugin) throws IOException {
        plugin.reload();
        sender.sendMessage(ChatColor.GREEN + "重载成功");
    }

    @Component
    public static class PluginTabComplete implements TabComplete {
        @Override
        public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
            String prefix = args[args.length - 1].toLowerCase();
            List<String> candidates = new ArrayList<>();
            for (Plugin plugin : PluginContext.getPlugins()) {
                if (plugin.getId().toLowerCase().startsWith(prefix)) {
                    candidates.add(plugin.getId());
                }
            }
            return candidates;
        }
    }

}
